package com.hq.ims.data.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hq.ims.data.config.verification.VdAdd;
import com.hq.ims.data.dao.CompanyServiceAreaMapper;
import com.hq.ims.data.entity.Area;
import com.hq.ims.data.entity.CompanyServiceArea;
import com.hq.ims.data.vo.CompanyServiceAreaMapstruct;
import com.hq.mybatis.base.BaseService;
import com.hq.mybatis.base.page.RequestPage;
import com.hq.utils.util.ObjectUtils;
import com.hq.web.utils.SpringBootValidateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 蒋柏平
 * @detail 企业服务区域 服务实现类
 * @since 20241022
 */
@Service
public class BaseCompanyServiceAreaService extends BaseService<CompanyServiceAreaMapper, CompanyServiceArea> {

    @Resource
    private BaseDictService baseDictService;
    @Resource
    private BaseCompanyService baseCompanyService;
    @Resource
    private BaseAreaService baseAreaService;


    /**
     * 分页
     */
    public List<CompanyServiceAreaMapstruct.CompanyServiceAreaVo> getByPage(
            RequestPage page,
            CompanyServiceAreaMapstruct.PageSearchVo searchVo
    ) {
        //验证
        LambdaQueryChainWrapper<CompanyServiceArea> lq = this.lambdaQuery();
        //查询条件

        //执行分页
        lq.page(page);

        List<CompanyServiceArea> dataList = page.getRecords();
        List<CompanyServiceAreaMapstruct.CompanyServiceAreaVo> voList = CompanyServiceAreaMapstruct.INSTANCE.toCompanyServiceAreaVoList(dataList);
        page.setRecords(voList);
        return voList;
    }

    /**
     * 添加
     */
    @Transactional(rollbackFor = Exception.class)
    public CompanyServiceArea addData(CompanyServiceAreaMapstruct.SaveVo vo) {
        //验证
        SpringBootValidateUtils.validate(vo, VdAdd.class);
        Area provinceArea = baseAreaService.getById(vo.getProvinceCode());
        ObjectUtils.isNullThrow(provinceArea, "省不存在");
        Area cityArea = baseAreaService.getById(vo.getCityCode());
        ObjectUtils.isNullThrow(cityArea, "市不存在");

        //账值新的对象
        CompanyServiceArea entity = new CompanyServiceArea();
        entity.setCompanyId(vo.getCompanyId());
        entity.setProvinceCode(provinceArea.getAreaId());
        entity.setProvinceName(provinceArea.getAreaName());
        entity.setCityCode(cityArea.getAreaId());
        entity.setCityName(cityArea.getAreaName());

        entity.insert();
        return entity;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    public void delByCompanyId(String companyServiceAreaId) {
        this.lambdaUpdate().eq(CompanyServiceArea::getCompanyId, companyServiceAreaId).remove();
    }

    /**
     * 获取原始数据详情
     */
    public CompanyServiceAreaMapstruct.CompanyServiceAreaVo getDetailVo(String companyServiceAreaId) {
        CompanyServiceArea entity = this.getById(companyServiceAreaId);
        CompanyServiceAreaMapstruct.CompanyServiceAreaVo vo = CompanyServiceAreaMapstruct.INSTANCE.toCompanyServiceAreaVo(entity);
        return vo;
    }
}